home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / misc.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  6KB  |  289 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <X11/Intrinsic.h>
  16. #include <X11/StringDefs.h>
  17. #include <X11/cursorfont.h>
  18. #include <X11/Xaw/Label.h>
  19. #include <ctype.h>
  20.  
  21. #include "bitmaps/background.xbm"
  22.  
  23. #ifndef XtNcursor
  24. #define XtNcursor     "cursor"
  25. #endif
  26.  
  27. #include "xpaint.h"
  28.  
  29. Widget GetToplevel(Widget w)
  30. {
  31.     Widget    p = w;
  32.  
  33.     while (w != None) {
  34.         p = w;
  35.         w = XtParent(w);
  36.     }
  37.     return p;
  38. }
  39.  
  40. Widget GetShell(Widget w)
  41. {
  42.     while (!XtIsShell(w))
  43.         w = XtParent(w);
  44.     return w;
  45. }
  46.  
  47. void SetIBeamCursor(Widget w)
  48. {
  49.     XtVaSetValues(w, XtVaTypedArg, 
  50.         XtNcursor, XtRString, "xterm", sizeof(Cursor), NULL);
  51. }
  52.  
  53. void SetCrossHairCursor(Widget w)
  54. {
  55.     XtVaSetValues(w, XtVaTypedArg, 
  56.         XtNcursor, XtRString, "crosshair", sizeof(Cursor), NULL);
  57. }
  58. void SetPencilCursor(Widget w)
  59. {
  60.     XtVaSetValues(w, XtVaTypedArg, 
  61.         XtNcursor, XtRString, "pencil", sizeof(Cursor), NULL);
  62. }
  63.  
  64. /*
  65. **  Some useful XRectangle computation code.
  66. */
  67. XRectangle *RectUnion(XRectangle *a, XRectangle *b)
  68. {
  69.     static XRectangle    out;
  70.     int            sx, ex, sy, ey;
  71.  
  72.     sx = MIN(a->x, b->x);
  73.     sy = MIN(a->y, b->y);
  74.     ex = MAX(a->x + a->width,  b->x + b->width);
  75.     ey = MAX(a->y + a->height, b->y + b->height);
  76.     
  77.     XYtoRECT(sx, sy, ex, ey, &out);
  78.     
  79.     return &out;
  80. }
  81.  
  82. XRectangle *RectIntersect(XRectangle *a, XRectangle *b)
  83. {
  84.     static XRectangle    out;
  85.     int            w, h;
  86.  
  87.     if (a == NULL || b == NULL)
  88.         return NULL;
  89.  
  90.     out.x = MAX(a->x, b->x);
  91.     out.y = MAX(a->y, b->y);
  92.     w     = MIN(a->x + a->width,  b->x + b->width)  - out.x;
  93.     h     = MIN(a->y + a->height, b->y + b->height) - out.y;
  94.  
  95.     if (w <= 0 || h <= 0)
  96.         return NULL;
  97.  
  98.     out.width  = w;
  99.     out.height = h;
  100.  
  101.     return &out;
  102. }
  103.  
  104. /*
  105. **
  106. */
  107. void GetPixmapWHD(Display *dpy, Drawable d, int *wth, int *hth, int *dth)
  108. {
  109.     Window        root;
  110.     int        x, y;
  111.     unsigned int    width, height, bw, depth;
  112.  
  113.     XGetGeometry(dpy, d, &root, &x, &y, &width, &height, &bw, &depth);
  114.  
  115.     if (wth != NULL) *wth = width;
  116.     if (hth != NULL) *hth = height;
  117.     if (dth != NULL) *dth = depth;
  118. }
  119.  
  120. /*
  121. **  Two useful functions to "cache" both a tiled background
  122. **    and a Xor gc
  123. */
  124. typedef struct bgList_s {
  125.     int            depth;
  126.     Pixmap            pixmap;
  127.     struct    bgList_s    *next;
  128. } BackgroundList;
  129.  
  130. static BackgroundList    *bgList = NULL;
  131.  
  132. Pixmap    GetBackgroundPixmap(Widget w)
  133. {
  134.     Widget        p, n;
  135.     Display        *dpy = XtDisplay(w);
  136.     int        depth;
  137.     Pixmap        pix;
  138.     Pixel        fg, bg;
  139.     BackgroundList    *cur;
  140.     Widget        tw;
  141.  
  142.     for (n = XtParent(p=w); !XtIsShell(n); n = XtParent(p=n));
  143.  
  144.     XtVaGetValues(p, XtNdepth, &depth, NULL);
  145.  
  146.     for (cur = bgList; bgList != NULL; bgList = bgList->next)
  147.         if (cur->depth == depth)
  148.             return cur->pixmap;
  149.  
  150.     tw = XtVaCreateWidget("junkWidget", labelWidgetClass, w, 
  151.                 XtNwidth, 1, 
  152.                 XtNheight, 1, 
  153.                 NULL);
  154.     XtVaGetValues(tw, XtNforeground, &fg, 
  155.                       XtNbackground, &bg, 
  156.               NULL);
  157.  
  158.     XtDestroyWidget(tw);
  159.  
  160.     pix = XCreatePixmapFromBitmapData(dpy, DefaultRootWindow(dpy),
  161.                 (char *)background_bits,
  162.                 background_width, background_height,
  163.                 bg,fg, depth);
  164.  
  165.     cur = XtNew(BackgroundList);
  166.     cur->next = bgList;
  167.     bgList = cur;
  168.     cur->depth = depth;
  169.     cur->pixmap = pix;
  170.     return pix;
  171. }
  172.  
  173. typedef struct gcList_s {
  174.     Widget            widget;
  175.     int            depth;
  176.     GC            gc;
  177.     struct    gcList_s    *next;
  178. } GCXList;
  179.  
  180. static GCXList    *gcList = NULL;
  181.  
  182. GC    GetGCX(Widget w)
  183. {
  184.     Widget        n = GetShell(w);
  185.     int        depth;
  186.     GCXList        *cur;
  187.     XGCValues    values;
  188.  
  189.     XtVaGetValues(n, XtNdepth, &depth, NULL);
  190.  
  191.     for (cur = gcList; gcList != NULL; gcList = gcList->next)
  192.         if (cur->depth == depth)
  193.             return cur->gc;
  194.  
  195.     values.function   = GXxor;
  196.     values.foreground = 1;
  197.  
  198.     cur = XtNew(GCXList);
  199.     cur->next = gcList;
  200.     gcList = cur;
  201.     cur->depth = depth;
  202.     cur->gc = XtGetGC(n, GCFunction|GCForeground, &values);
  203.     return cur->gc;
  204. }
  205.  
  206. /*
  207. **  Argv parsing routines
  208. */
  209.  
  210. static char    *nextArg(char *str, char **start)
  211. {
  212.     char    *cp;
  213.     int    flg;
  214.  
  215.     while (isspace(*str))
  216.         str++;
  217.     if (start != NULL)
  218.         *start = str;
  219.     if (*str == '\0')
  220.         return NULL;
  221.     if (*str == '\'' || *str == '"') {
  222.         char    delim = *str;
  223.         if (start != NULL)
  224.             (*start)++;
  225.         cp = ++str;
  226.         while (*str != '\0' && *str != delim) {
  227.             if (*str == '\\') {
  228.                 if (*++str == '\0')
  229.                     continue;
  230.             }
  231.             *cp++ = *str++;
  232.         }
  233.         *cp = '\0';
  234.     } else {
  235.         while (!isspace(*str) && *str != '\0')
  236.             str++;
  237.     }
  238.  
  239.     flg = (*str != '\0');
  240.     *str = '\0';
  241.     return str + (flg ? 1 : 0);
  242. }
  243.  
  244. void    StrToArgv(char *str, int *argc, char **argv)
  245. {
  246.     int    t;
  247.     char    *cp;
  248.  
  249.     if (argc == NULL)
  250.         argc = &t;
  251.  
  252.     *argc = 0;
  253.     for (cp = str; cp != NULL; (*argc)++)
  254.         cp = nextArg(cp, argv == NULL ? NULL : &argv[*argc]);
  255.  
  256.     if (argv != NULL)
  257.         argv[--(*argc)] = NULL;
  258. }
  259.  
  260. /*
  261. **  Create a XImage
  262. */
  263. XImage    *NewXImage(Display *dpy, Visual *visual, int depth, int width, int height)
  264. {
  265.     XImage    *xim;
  266.     int    pad;
  267.  
  268.     if (depth > 16)
  269.         pad = 32;
  270.     else if (depth > 8)
  271.         pad = 16;
  272.     else
  273.         pad = 8;
  274.  
  275.     xim = XCreateImage(dpy, visual, depth, ZPixmap, 0, NULL, width, height, pad, 0);    
  276.  
  277.     if (xim == NULL)
  278.         return NULL;
  279.  
  280.     xim->data = (char*)XtMalloc(xim->bytes_per_line * height);
  281.  
  282.     if (xim->data == NULL) {
  283.         XDestroyImage(xim);
  284.         xim = NULL;
  285.     }
  286.  
  287.     return xim;
  288. }
  289.